package main

import (
	"GraduationProject/public/db"
	"GraduationProject/public/models"
	"GraduationProject/public/tools"
	_ "GraduationProject/public/tools"
	"GraduationProject/webserver/controllers/admin/account"
	"GraduationProject/webserver/controllers/admin/columns"
	adminSearch "GraduationProject/webserver/controllers/admin/data_search"
	"GraduationProject/webserver/controllers/admin/home"
	"GraduationProject/webserver/controllers/admin/machine"
	"GraduationProject/webserver/controllers/admin/nsq_setting"
	"GraduationProject/webserver/controllers/admin/seed"
	"GraduationProject/webserver/controllers/admin/user_manager"
	"GraduationProject/webserver/controllers/admin/worker"
	"GraduationProject/webserver/controllers/auth"
	"GraduationProject/webserver/controllers/public"
	userColumns "GraduationProject/webserver/controllers/user/columns"
	"GraduationProject/webserver/controllers/user/data_search"
	userSeed "GraduationProject/webserver/controllers/user/seed"
	"GraduationProject/webserver/controllers/user/self"
	"GraduationProject/webserver/controllers/user/upload"
	"encoding/gob"
	"flag"
	"fmt"
	"github.com/gin-gonic/contrib/sessions"
	"github.com/gin-gonic/gin"
	"net/http"
	"os"
)

var (
	syncDB = flag.Bool("syncdb", false, "sync database.")
)

/*
	WebServer
*/
func init() {
	flag.Parse()

	fmt.Println(*syncDB)

	if *syncDB {
		db.SyncDB()
		os.Exit(0)
	}

	db.ConnectDB()
	db.RedisConnect()
	db.ElasticSearchConnect()
}

func main() {
	router := gin.New()

	router.LoadHTMLGlob("./views/*")
	router.StaticFS("/static/", http.Dir("./static/"))

	gob.Register(models.User{})
	store, err := sessions.
		NewRedisStore(100, "tcp",
			tools.Conf.Get("redis_addr")+":"+tools.Conf.Get("redis_port"),
			tools.Conf.Get("redis_pass"),
			[]byte("secret"))
	if err != nil {
		panic(err)
	}
	store.Options(sessions.Options{
		Path:   "/",
		MaxAge: 60 * 60,
	})

	router.Use(sessions.Sessions("session", store))

	router.GET("/", public.Index)
	router.POST("/login", auth.Login)
	router.POST("/user/info", auth.UserInfo)
	router.POST("/logout", auth.Logout)
	router.POST("/register", auth.Register)
	router.POST("/register/handler", account.RegisterHandler)
	router.POST("/register/list", account.RegisterList)

	router.POST("/home", home.Home)

	router.POST("/upload/image", upload.Image)

	router.POST("/user/gets", user_manager.GetUsers)
	router.POST("/user/setData", user_manager.SetUserData)
	router.POST("/user/SetAvatar", user_manager.SetAvatarUrl)
	router.POST("/user/changeStatus", user_manager.ChangeUserStatus)

	router.POST("/machine/gets", machine.GetMachines)
	router.POST("/machine/SetStatus", machine.SetMachineStatus)

	router.POST("/seed/gets", userSeed.Seeds)
	router.POST("/seed/add", seed.Add)
	router.POST("/seed/del", seed.Delete)

	router.POST("/columns/add", columns.Add)
	router.POST("/columns/del", columns.Delete)
	router.POST("/columns/get", userColumns.Get)
	router.POST("/columns/gets", userColumns.Columns)

	router.POST("/nsq/gets/worker", nsq_setting.NsqSettingsWorker)
	router.POST("/nsq/gets/seed", nsq_setting.NsqSettingsSeed)
	router.POST("/nsq/pause", nsq_setting.Pause)
	router.POST("/nsq/begin", nsq_setting.Begin)

	router.POST("/worker/gets", worker.Workers)
	router.POST("/worker/bind", worker.Bind)
	router.POST("/worker/bindCancel", worker.BindCancel)
	router.POST("/worker/pause", worker.Pause)
	router.POST("/worker/begin", worker.Begin)

	router.POST("/data/search", data_search.Search)

	router.POST("/self/setData", self.SetSelfData)
	router.POST("/self/setAvatar", self.SetAvatarUrl)
	router.POST("/self/setPassword", self.SetPassword)

	router.GET("/output", adminSearch.OutPut)

	if err := router.Run(tools.Conf.Get("port")); err != nil {
		panic(err)
	}
}
